#include <stdio.h>
#include <pthread.h>

#define DEBUG_FPS_BEGIN()   static struct timeval tpstart,tpend;  float timeuse; \
                             gettimeofday(&tpstart,NULL); \

#define DEBUG_FPS_END(_FUN_,_MAX_) gettimeofday(&tpend,NULL); \
                        timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+ tpend.tv_usec-tpstart.tv_usec; \
                       if(timeuse > _MAX_) printf(#_FUN_ " Used Time:%f ms\n",timeuse/1000.0); \
                       tpstart = tpend;
 
void Hello(){
  int i;
  for(i=0; i<4; i++)
    printf("Hello, world.\n");
}

void Hellos(){
    int i;
    #pragma omp parallel for
    for(i=0; i<4; i++)
      printf("Hello, world.\n");
}

void caculate(){
  const int N = 990000;
  int i, a[N];

  for (i = 0; i < N; i++)
    a[i] = 2 * i;  
}

void caculates(){
  const int N = 990000;
  int i, a[N];

  #pragma omp parallel for
  for (i = 0; i < N; i++)
    a[i] = 2 * i;  
}

void thread(){
  #pragma omp parallel
  printf("Thread %d.\n", pthread_self());
}

void threads(){
  int th_id, nthreads;
  #pragma omp parallel private(th_id)
  {
    th_id = omp_get_thread_num();
    printf("Hello World from thread %d\n", th_id);
    #pragma omp barrier
    if ( th_id == 0 ) {
      nthreads = omp_get_num_threads();
      printf("There are %d threads\n",nthreads);
    }
  }
}

#define W 1920 
#define H 1080
static uint8_t pixels[W*H*4];
void swap_bits(){
  int i,j;
  uint32_t* p = pixels; 

  for(i=0; i<H; i++){    
      for(j=0; j<W; j++, p++){
          *p = ((*p&0xFF) << 16) | ((*p&0xFF0000) >> 16) | (*p&0xFF00FF00);
      }
  }
}

void swap_bits_p(){
  int i,j;
  uint32_t* p = pixels; 

  #pragma omp parallel for
  for(i=0; i<H; i++){
      for(j=0; j<W; j++){
          p[i*W+j] = (( p[i*W+j]&0xFF) << 16) | (( p[i*W+j]&0xFF0000) >> 16) | ( p[i*W+j]&0xFF00FF00);
      }
  }
}

int main(void)
{
  DEBUG_FPS_BEGIN()
  Hello();
  DEBUG_FPS_END(HELLO, 0);
  Hellos();
  DEBUG_FPS_END(HELLOS, 0);
  caculate();
  DEBUG_FPS_END(caculate, 0);
  caculates();
  DEBUG_FPS_END(caculates, 0);
  swap_bits();
  DEBUG_FPS_END(swap_bits, 0);
  swap_bits_p();
  DEBUG_FPS_END(swap_bits_p, 0);
  printf("\n\ndone.\n");
  return 0;
}